/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.txweb.dao.impl;

import com.jspx.sober.Criteria;
import com.jspx.sober.criteria.expression.Expression;
import com.jspx.sober.criteria.projection.Projections;
import com.jspx.sober.jdbc.JdbcOperations;
import com.jspx.sober.ssql.SSqlExpression;
import com.jspx.txweb.dao.GenericDAO;
import com.jspx.utils.StringUtil;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: chenYuan
 * Date: 2008-8-4
 * Time: 17:27:49
 * com.jspx.txweb.dao.impl.GenericDAOImpl
 */
public class GenericDAOImpl extends JdbcOperations implements GenericDAO
{
    public GenericDAOImpl()
    {
           
        
    }


    /**
     *
     * @param T  class对象
     * @param field  字段
     * @param find 查询条件
     * @param term  条件
     * @param uid  用户
     * @param sortString  排序
     * @param page  页数
     * @param count  行数
     * @param load   载入关联
     * @return  返回列表
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public List getList(
            Class T,
            String field,
            String find,
            String term,
            long uid,
            String sortString,
            int page, int count, boolean load) throws Exception
    {
        String sort;
        if (StringUtil.isNULL(sortString))
        {
            sort = "sortType:D;createDate:D";
        } else
        {
            sort = sortString;
        }
        Criteria criteria = createCriteria(T);
        if (!StringUtil.isNULL(field)&&!StringUtil.isNULL(find))
        {
            criteria = criteria.add( Expression.like(field, "%" + find + "%"));
        }
        if (uid > 0) {
            criteria = criteria.add(Expression.eq("putUid", uid));
        }
        criteria = SSqlExpression.getTermExpression(criteria, term);
        criteria = SSqlExpression.getSortOrder(criteria, sort);
        return criteria.setCurrentPage(page).setTotalCount(count).list(load);
    }

    /**
     *
     * @param T   类
     * @param field   字段
     * @param find     查询
     * @param term   条件
     * @param uid    用户id
     * @return   得到长度
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public int getCount(
            Class T,
            String field,
            String find,
            String term,
            long uid
            ) throws Exception
    {
        Criteria criteria = createCriteria(T);
        if (!StringUtil.isNULL(field)&&!StringUtil.isNULL(find))
        {
            criteria = criteria.add( Expression.like(field, "%" + find + "%"));
        }
        if (uid > 0) {
            criteria = criteria.add(Expression.eq("putUid", uid));
        }

        return SSqlExpression.getTermExpression(criteria, term).setProjection(Projections.rowCount()).intUniqueResult();
    }

}